package cmd

import (
	"context"
	"go_fream/internal/consts"
	"go_fream/internal/controller/learn_fream"
	"go_fream/internal/controller/performance"
	"go_fream/internal/controller/postman"
	"go_fream/internal/controller/user"

	"go_fream/internal/controller/hello"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
)

func test(req *ghttp.Request) {
	req.Response.Writefln("test route")
}

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()

			// 添加测试路由
			s.BindHandler("GET:/test", test)
			//s.BindHandler("POST:/he", func(req *ghttp.Request) {
			//	req.Response.Writefln("hello GF")
			//})
			// 修改命名解决下方分组命名冲突
			//new_hello := hello.NewHello()
			//s.BindHandler("/say", new_hello.SayHello)
			// 设置文档重定向
			s.BindHandler("GET:/postman", postman.New().Postman)
			s.BindHandler("GET:/docs", postman.New().GetSwagger)
			s.BindHandler("GET:/gofream", postman.New().GetDocument)
			// 设置路由绑定对象
			//s.BindObject("/user", user.New(), "Add")  //只绑定Add

			//s.BindObject("/user", user.New())
			// 默认路由
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Bind(
					hello.NewHello(),
					//learn_fream.NewLearn(),
					learn_fream.NewLearnHtml(),
				)

			})

			s.Group("/api", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Group("/performance", func(group1 *ghttp.RouterGroup) {
					group1.Bind(
						performance.New(),
					)
				})
				group.Group("/user", func(group2 *ghttp.RouterGroup) {
					group2.Bind(
						user.New(),
					)
				})

			})

			//# 设置静态路由
			s.SetServerRoot("resource/public")
			// 设置swaggerUI
			s.SetSwaggerUITemplate(consts.MySwaggerUITemplate)
			//s.SetSwaggerUITemplate(MyOpenapiUITemplate)
			//s.SetSwaggerUITemplate(MyScalarUITemplate)
			s.Run()
			return nil
		},
	}
)
